/**************************************************************************(C)*
 *
 *  This file is part of the GreenHills project
 *  Copyright (C) 2009  Tieto Corporation
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  Not for commmercial use. Only with Tieto permission.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *************************************************************************(C)*/

#include "../msgclientsocketconnection.h"
#include "io/ioexception.h"
#include <qglobal.h>

#if defined Q_OS_LINUX
    #include <sys/ioctl.h>
    #include <sys/socket.h>
    #include <netdb.h>
    #include <arpa/inet.h>
    #include <netinet/in.h>
    #include <memory.h>
#elif defined Q_OS_WIN32
#elif defined Q_OS_WINCE
#endif

using namespace greenhills::net;

CMsgClientSocketConnection::CMsgClientSocketConnection()
        : CMsgSocketConnection(INVALID_SOCKET)
{
}

CMsgClientSocketConnection::~CMsgClientSocketConnection()
{
#if defined Q_OS_WIN32 || defined Q_OS_WINCE
    WSACleanup();
#endif
}

void CMsgClientSocketConnection::open(const char *pHost,
                                      unsigned short pPort)
{
#if defined Q_OS_WIN32 || defined Q_OS_WINCE
    WORD versionRequested = MAKEWORD(1, 1);
    WSADATA data;

    if (WSAStartup(versionRequested, &data) != 0)
    {
        throw io::CIOException("Can't initialize socket library");
    }
#endif

    if (mSocket != INVALID_SOCKET) {
        throw io::CIOException("Socket already opened. "
                               "It has to be closed first");
    }

    struct sockaddr_in addr;
    struct hostent *host;

    if ((mSocket = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
        throw io::CIOException("Can't create socket");
    }

    if ((host = gethostbyname(pHost)) == 0) {
        throw io::CIOException("Can't resolve host name");
    }

    memset(&addr, 0x00, sizeof(addr));
    addr.sin_family = AF_INET;

#if defined Q_OS_LINUX
    addr.sin_addr.s_addr = *((in_addr_t*) host->h_addr);
#elif defined Q_OS_WIN32
    addr.sin_addr = *((in_addr*) host->h_addr);
#elif defined Q_OS_WINCE
    addr.sin_addr = *((in_addr*) host->h_addr);
#endif

    addr.sin_port = htons(pPort);

    if (connect(mSocket, (struct sockaddr*) &addr, sizeof(addr)) < 0) {
        throw io::CIOException("Can't connect to host");
    }

    unsigned long mode = 1;
#if defined Q_OS_LINUX
    ioctl(mSocket, FIONBIO, &mode);
#elif defined Q_OS_WIN32
    ioctlsocket(mSocket, FIONBIO, &mode);
#elif defined Q_OS_WINCE
    ioctlsocket(mSocket, FIONBIO, &mode);
#endif
}
